package com.eduhzy.ttw.teacher.mvp.presenter;

import android.app.Application;
import android.os.Message;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.entity.RegisterData;
import com.eduhzy.ttw.commonsdk.entity.RegisterRequestData;
import com.eduhzy.ttw.commonsdk.utils.Utils;
import com.eduhzy.ttw.teacher.mvp.contract.RegisterContract;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.RxLifecycleUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.EventBus;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


@ActivityScope
public class RegisterPresenter extends BasePresenter<RegisterContract.Model, RegisterContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public RegisterPresenter(RegisterContract.Model model, RegisterContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void requestRegister(RegisterRequestData data, int loginType) {
        Observable<CommonData<RegisterData>> registerData = null;
        if (loginType == 3) {
            registerData = mModel.getRegisterData(data);
        } else if (loginType == Constants.LOGIN_THIRD) {
            registerData = mModel.getThirdRegisterData(data);
        }
        registerData.subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3, 2))
                .doOnSubscribe(disposable -> mRootView.showLoading())
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<RegisterData>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<RegisterData> commonData) {
                        if (commonData.isResult()) {
                            if (loginType == 3) {
                                SPUtils.getInstance().put(Constants.USERNAME, data.getYddh());
                                SPUtils.getInstance().put(Constants.PASSWORD, data.getPassWord());
                                RxUtil.showTipDialog(ActivityUtils.getTopActivity(), QMUITipDialog.Builder.ICON_TYPE_SUCCESS, "注册成功", 1500);
                                ARouter.getInstance().build(RouterHub.APP_LOGINACTIVITY).withBoolean(AppManager.IS_NOT_ADD_ACTIVITY_LIST, true).navigation();
                                ArmsUtils.killAll();
                            } else {
                                SPUtils.getInstance().put(Constants.TOKEN, commonData.getData().getAuthId(), SPUtils.getInstance().contains(Constants.TOKEN));
                                SPUtils.getInstance().put(Constants.USERID, String.valueOf(commonData.getData().getUserId()), SPUtils.getInstance().contains(Constants.USERID));
                                SPUtils.getInstance().put(Constants.USERNICKNAME, String.valueOf(commonData.getData().getUserNickName()), SPUtils.getInstance().contains(Constants.USERNICKNAME));
                                Message msg = Message.obtain();
                                msg.what = EventBusTags.LOGIN_SUCCESS;
                                EventBus.getDefault().post(msg, EventBusTags.LOGIN);
                                Utils.navigationWithTitle(RouterHub.APP_HOMEACTIVITY, commonData.getData().getUserNickName());
                                mRootView.killMyself();
                            }
                        }else{
                            mRootView.showMessage(commonData.getMessage());
                        }
                    }
                });
    }
}
